package com.ruoyi.buss.report.service.impl;

import com.ruoyi.buss.report.domain.ReportWgzyscGrLsb;
import com.ruoyi.buss.report.domain.ReportWgzyscShLsb;
import com.ruoyi.buss.report.mapper.ReportWgzyscGrLsbMapper;
import com.ruoyi.buss.report.mapper.ReportWgzyscShLsbMapper;
import com.ruoyi.buss.report.service.IReportWgZyscLsbService;
import com.ruoyi.buss.utils.DateUtilss;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 网格社区报表Service业务层处理
 *
 * @author sxy
 * @date 2021-07-16
 */
@Service
public class ReportWgZyscLsbServiceImpl implements IReportWgZyscLsbService {


    @Autowired
    private ReportWgzyscGrLsbMapper reportWgzyscGrLsbMapper;

    @Autowired
    private ReportWgzyscShLsbMapper reportWgzyscShLsbMapper;
    /**
     * 网格专业市场4.2.1-1商户类
     */
    public List<ReportWgzyscShLsb> wgzyscywjdb(ReportWgzyscShLsb reportWgzyscShLsb, LoginUser userInfo) {


        if (!org.apache.commons.lang3.StringUtils.isEmpty(reportWgzyscShLsb.getSjrq())) {
            reportWgzyscShLsb.setSjrq(reportWgzyscShLsb.getSjrq().replace("-", ""));
        }
        // 获取上个月最后一天
        reportWgzyscShLsb
                .setMonthEnd(reportWgzyscShLsb.getSjrq() == null || reportWgzyscShLsb.getSjrq().length() < 8 ? DateUtilss.getLastDayOfMonth(Integer.valueOf(DateUtils.getMMddStr().substring(0, 2)) - 1) : Integer.valueOf(reportWgzyscShLsb.getSjrq().substring(4, 6)) == 1 ? Integer.valueOf(reportWgzyscShLsb.getSjrq().substring(0, 4)) - 1 + "1231" : reportWgzyscShLsb.getSjrq().substring(0, 4) + DateUtilss.getLastDayOfMonths(Integer.valueOf(reportWgzyscShLsb.getSjrq().substring(4, 6)) - 1));
        // 获取去年最后一天
        reportWgzyscShLsb.setYearEnd(reportWgzyscShLsb.getSjrq() == null || reportWgzyscShLsb.getSjrq().length() < 8 ? DateUtilss.getLastYear(new Date(), -1) : Integer.valueOf(reportWgzyscShLsb.getSjrq().substring(0, 4)) - 1 + "1231");
        if (reportWgzyscShLsb.getStartTime() == null) {
            // 如果开始将时间为空 ,则设置开始时间为去年最后一天
            reportWgzyscShLsb.setStartTime(DateUtilss.getLastYear(new Date(), -1));
        }
        if (reportWgzyscShLsb.getEndTime() == null) {
            // 如果结束时间为空则设置结束日期到昨天
            reportWgzyscShLsb.setEndTime(new SimpleDateFormat("yyyyMMdd").format(DateUtilss.getdate(-1)));
        }

        List<ReportWgzyscShLsb> doList = new ArrayList<ReportWgzyscShLsb>();
        String orgId = userInfo.getSysUser().getDept().getOrgId();
        String userId = userInfo.getSysUser().getUserName();
        //如果是admin用户不做处理
        if (userInfo.getRoles().contains("admin")) {

        } else if (userInfo.getRoles().contains("zhhz")) {
            reportWgzyscShLsb.setOrgId(orgId);
        } else if (userInfo.getRoles().contains("common")) {
            reportWgzyscShLsb.setOrgId(orgId);
            reportWgzyscShLsb.setUserId(userId);
        } else {
            return doList;
        }
        doList = reportWgzyscShLsbMapper.wgzyscywjdb(reportWgzyscShLsb);
        ReportWgzyscShLsb vo = reportWgzyscShLsbMapper.wgzyscywjdbhz(reportWgzyscShLsb).get(0);
        vo.setOrgName("总行汇总数据");
        vo.setSjrq("总行汇总数据");
        doList.add(vo);

//            ArrayList<String> sjrqs = DateUtilss.getThreeYearFourQuarterAndMonth();


        return doList;
    }


    /**
     * 网格专业市场4.2.1-2个人类
     */
    public List<ReportWgzyscGrLsb> wgzyscgrywjdb(ReportWgzyscGrLsb reportWgzyscGrLsb, LoginUser userInfo) {


        if (!org.apache.commons.lang3.StringUtils.isEmpty(reportWgzyscGrLsb.getSjrq())) {
            reportWgzyscGrLsb.setSjrq(reportWgzyscGrLsb.getSjrq().replace("-", ""));
        }
        // 获取上个月最后一天
        reportWgzyscGrLsb
                .setMonthEnd(reportWgzyscGrLsb.getSjrq() == null || reportWgzyscGrLsb.getSjrq().length() < 8 ? DateUtilss.getLastDayOfMonth(Integer.valueOf(DateUtils.getMMddStr().substring(0, 2)) - 1) : Integer.valueOf(reportWgzyscGrLsb.getSjrq().substring(4, 6)) == 1 ? Integer.valueOf(reportWgzyscGrLsb.getSjrq().substring(0, 4)) - 1 + "1231" : reportWgzyscGrLsb.getSjrq().substring(0, 4) + DateUtilss.getLastDayOfMonths(Integer.valueOf(reportWgzyscGrLsb.getSjrq().substring(4, 6)) - 1));
        // 获取去年最后一天
        reportWgzyscGrLsb.setYearEnd(reportWgzyscGrLsb.getSjrq() == null || reportWgzyscGrLsb.getSjrq().length() < 8 ? DateUtilss.getLastYear(new Date(), -1) : Integer.valueOf(reportWgzyscGrLsb.getSjrq().substring(0, 4)) - 1 + "1231");
        if (reportWgzyscGrLsb.getStartTime() == null) {
            // 如果开始将时间为空 ,则设置开始时间为去年最后一天
            reportWgzyscGrLsb.setStartTime(DateUtilss.getLastYear(new Date(), -1));
        }
        if (reportWgzyscGrLsb.getEndTime() == null) {
            // 如果结束时间为空则设置结束日期到昨天
            reportWgzyscGrLsb.setEndTime(new SimpleDateFormat("yyyyMMdd").format(DateUtilss.getdate(-1)));
        }

        List<ReportWgzyscGrLsb> doList = new ArrayList<ReportWgzyscGrLsb>();
        String orgId = userInfo.getSysUser().getDept().getOrgId();
        String userId = userInfo.getSysUser().getUserName();
        //如果是admin用户不做处理
        if (userInfo.getRoles().contains("admin")) {

        } else if (userInfo.getRoles().contains("zhhz")) {
            reportWgzyscGrLsb.setOrgId(orgId);
        } else if (userInfo.getRoles().contains("common")) {
            reportWgzyscGrLsb.setOrgId(orgId);
            reportWgzyscGrLsb.setUserId(userId);
        } else {
            return doList;
        }
        doList = reportWgzyscGrLsbMapper.wgzyscgrywjdb(reportWgzyscGrLsb);
        List<ReportWgzyscGrLsb> list = reportWgzyscGrLsbMapper.wgzyscgrywjdbhz(reportWgzyscGrLsb);
        ReportWgzyscGrLsb vo = list.get(0);
        vo.setOrgName("总行汇总数据");
        doList.add(vo);
        return doList;
    }

}
